package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.*;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ISchoolClassesService;
import com.ruoyi.system.service.ISchoolPayrecordService;
import com.ruoyi.system.service.ISchoolStudentService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.system.service.ISchoolConsumeService;

import javax.servlet.http.HttpServletResponse;

/**
 * 耗课信息Controller
 * 
 * @author ruoyi
 * @date 2022-11-19
 */
@RestController
@RequestMapping("/consume")
public class SchoolConsumeController extends BaseController
{
    @Autowired
    private ISchoolConsumeService schoolConsumeService;

    @Autowired
    private ISchoolStudentService schoolStudentService;

    @Autowired
    private ISchoolClassesService schoolClassesService;

    @Autowired
    private ISchoolPayrecordService schoolPayrecordService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询耗课信息列表
     */
    @RequiresPermissions("business:consume:list")
    @GetMapping("/list")
    public TableDataInfo list(SchoolConsume schoolConsume)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolConsume.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolConsume> list = schoolConsumeService.selectSchoolConsumeList(schoolConsume);
        return getDataTable(list);
    }

    @GetMapping("/listTj")
    public TableDataInfo listTj(SchoolConsume schoolConsume)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolConsume.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolConsumeTj> list = schoolConsumeService.selectSchoolConsumeTjList(schoolConsume);
        return getDataTable(list);
    }

    @GetMapping("/listXsTj")
    public TableDataInfo listXsTj(SchoolConsume schoolConsume)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolConsume.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolConsumeTj> list = schoolConsumeService.selectSchoolConsumeTjXsList(schoolConsume);
        return getDataTable(list);
    }

    /**
     * 导出耗课信息列表
     */
    @RequiresPermissions("business:consume:export")
    @Log(title = "耗课信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, SchoolConsume schoolConsume)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolConsume.setSchoolId(user.getDeptId());
        }
        List<SchoolConsume> list = schoolConsumeService.selectSchoolConsumeList(schoolConsume);
        ExcelUtil<SchoolConsume> util = new ExcelUtil<SchoolConsume>(SchoolConsume.class);
        util.exportExcel(response, list, "耗课信息");
    }

    /**
     * 获取耗课信息详细信息
     */
    @RequiresPermissions("business:consume:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolConsumeService.selectSchoolConsumeById(id));
    }

    @RequiresPermissions("business:consume:queryHk")
    @GetMapping(value = "/hk")
    public AjaxResult getHkInfo(SchoolConsume schoolConsume)
    {
        SchoolStudent params = new SchoolStudent();
        params.setSchoolId(schoolConsume.getSchoolId());
        params.setStudentId(schoolConsume.getStudentId());
        List<SchoolStudent> studentList = schoolStudentService.selectSchoolStudentList(params);
        SchoolStudent studentInfo = studentList.get(0);
        List<SchoolClasses> classesList = new ArrayList<>();
        for (SchoolStudent item:studentList) {
            SchoolClasses schoolClasses = schoolClassesService.selectSchoolClassesById(item.getClassesId());
            classesList.add(schoolClasses);
        }
        studentInfo.setClassesList(classesList);
        studentInfo.setConsumeAmount(classesList.get(0).getAmount());
        return AjaxResult.success(studentInfo);
    }

    @RequiresPermissions("business:consume:queryHk")
    @GetMapping(value = "/hkId/{ids}")
    public AjaxResult getHkIdInfo(@PathVariable Long[] ids)
    {
        SchoolStudent studentInfoRtn = new SchoolStudent();
        List<Long> schoolGetList = new ArrayList<>();
        List<SchoolClasses> classesGetList = new ArrayList<>();
        List<SchoolStudent> studentGetList = new ArrayList<>();
        for (Long id:ids) {
            SchoolPayrecord payrecord = schoolPayrecordService.selectSchoolPayrecordById(id);
            schoolGetList.add(payrecord.getSchoolId());
            SchoolStudent params = new SchoolStudent();
            params.setSchoolId(payrecord.getSchoolId());
            params.setStudentId(payrecord.getStudentId());
            List<SchoolStudent> studentRtnList = schoolStudentService.selectSchoolStudentList(params);
            for (SchoolStudent studentInfo:studentRtnList) {
                SysUser sysUser = sysUserService.selectUserById(studentInfo.getStudentId());
                studentInfo.setXsUsername(sysUser.getUserName());
                studentInfo.setXsPhone(sysUser.getPhonenumber());
                studentGetList.add(studentInfo);
            }
        }
        for (SchoolStudent item:studentGetList) {
            SchoolClasses schoolClasses = schoolClassesService.selectSchoolClassesById(item.getClassesId());
            classesGetList.add(schoolClasses);
        }
        List<Long> schoolList = new ArrayList(new HashSet(schoolGetList));
        List<SchoolStudent> studentList = new ArrayList(new HashSet(studentGetList));
        List<SchoolClasses> classesList = new ArrayList(new HashSet(classesGetList));
        studentInfoRtn.setSchoolId(schoolList.get(0));
        studentInfoRtn.setSchoolList(schoolList);
        studentInfoRtn.setClassesList(classesList);
        studentInfoRtn.setClassesId(classesList.get(0).getId());
        studentInfoRtn.setConsumeAmount(classesList.get(0).getAmount());
        List<SchoolStudent> studentShowList = new ArrayList<>();
        for (SchoolStudent obj:studentList) {
            if (studentInfoRtn.getClassesId().equals(obj.getClassesId())) {
                studentShowList.add(obj);
            }
        }
        studentInfoRtn.setStudentList(studentShowList);
        return AjaxResult.success(studentInfoRtn);
    }

    /**
     * 新增耗课信息
     */
    @RequiresPermissions("business:consume:add")
    @Log(title = "耗课信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolConsume schoolConsume)
    {
        return toAjax(schoolConsumeService.insertSchoolConsume(schoolConsume));
    }

    /**
     * 修改耗课信息
     */
    @RequiresPermissions("business:consume:edit")
    @Log(title = "耗课信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolConsume schoolConsume)
    {
        return toAjax(schoolConsumeService.updateSchoolConsume(schoolConsume));
    }

    /**
     * 删除耗课信息
     */
    @RequiresPermissions("business:consume:remove")
    @Log(title = "耗课信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolConsumeService.deleteSchoolConsumeByIds(ids));
    }
}
